package cym;

public class QuickSort {

    public static int P(int[] r, int low, int high) {
        int i = low, j = high;
        int pivot = r[i];
        while (i < j) {
            while (i < j && r[j] > pivot) {
                j--;
            }
            if (i < j) {
                r[i] = r[j];
                i++;
            }
            while (i < j && r[i] < pivot) {
                i++;
            }
            if (i < j) {
                r[j] = r[i];
                j--;
            }
        }
        r[i] = pivot;
        return i;
    }

    public static void sort(int[] r, int low, int high) {
        int pivot = P(r, low, high);
        sort(r, low, pivot - 1);
        sort(r, pivot + 1, high);
        display(r);
    }

    public static void display(int[] r) {
        for (int item : r) {
            System.out.print(item + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] r = new int[]{3, 5, 5, 4, 120, 43, 1, 76, 78, 12, 346};
        QuickSort.sort(r, 0, r.length - 1);
        QuickSort.display(r);
    }

}
